package no.hib.bridge.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import no.hib.bridge.beans.LinkBean;
import no.hib.bridge.net.SupedoHttpClient;

import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SupedoClientGUI extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField tfAddress;
	private JTextField tfName;
	private JTextField tfLocation;
	private JTextField tfSection;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SupedoClientGUI dialog = new SupedoClientGUI();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SupedoClientGUI() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[][grow]", "[][][][][][][][][][][][]"));
		{
			JLabel lblAddress = new JLabel("Address:");
			contentPanel.add(lblAddress, "cell 1 1");
		}
		{
			JLabel lblHttp = new JLabel("http://");
			contentPanel.add(lblHttp, "cell 0 2,alignx trailing");
		}
		{
			tfAddress = new JTextField();
			contentPanel.add(tfAddress, "cell 1 2,growx");
			tfAddress.setColumns(10);
		}
		{
			JLabel lblName = new JLabel("Name:");
			contentPanel.add(lblName, "cell 1 4");
		}
		{
			tfName = new JTextField();
			contentPanel.add(tfName, "cell 1 5,growx");
			tfName.setColumns(10);
		}
		{
			JLabel lblLocation = new JLabel("Location:");
			contentPanel.add(lblLocation, "cell 1 7");
		}
		{
			tfLocation = new JTextField();
			contentPanel.add(tfLocation, "cell 1 8,growx");
			tfLocation.setColumns(10);
		}
		{
			JLabel lblSection = new JLabel("Section:");
			contentPanel.add(lblSection, "cell 1 10");
		}
		{
			tfSection = new JTextField();
			contentPanel.add(tfSection, "cell 1 11,growx");
			tfSection.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						sendLink();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						close();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	public void close(){
		this.dispose();
	}
	
	public void sendLink(){
		LinkBean link = new LinkBean();
		link.setAddress(tfAddress.getText());
		link.setName(tfName.getText());
		link.setLocation(tfLocation.getText());
		link.setSection(tfSection.getText());
		
		SupedoHttpClient client = new SupedoHttpClient();
		try {
			client.sendLinkToSupedo(link);
			close();
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Cant connect to Supedo Server", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

}
